﻿//This is a useful class for managing animations with no code on the frames
//This class allows you to trigger events based on frame names so that artists will be able to easily reskin code
//please use this class unless you have another solution that provides these elements

import mx.utils.Delegate;

class arkadium.ArkMc extends MovieClip
{
	public var _bPlaying:Boolean;
	private var _aEventList:Array;
	private var _fEndFunction:Function;
	private var _nTotalframes:Number;

	public function ArkMc()
	{
		onInitialize();
	}
	
	function onInitialize(){
		this.stop();
		_bPlaying = false;
		_aEventList = new Array();
		_nTotalframes = this._totalframes
		_fEndFunction =  endFunction;
	}
	
	function playAnimation(pFrame){
		if(pFrame != undefined) this.gotoAndPlay(pFrame);
		else this.play();
		_bPlaying = true;
		this.onEnterFrame = this.trackProgress;
	}
	
	function endFunction(){
		trace("endfunction")
		stopAnimation()
	}
	
	function stopAnimation(pFrame){
		if(pFrame != undefined) this.gotoAndStop(pFrame);
		else this.stop();
		_bPlaying = false;
		delete this.onEnterFrame;
	}
	
	function setEndFunction(fEndFunction:Function){
		_fEndFunction = fEndFunction;
	}
	
	function trackProgress(){
		
		if(this._currentframe >= _nTotalframes)_fEndFunction();
		else if(_aEventList[this._currentframe ] != undefined){
			trace("trying to do "+ _aEventList[this._currentframe ])
			_aEventList[this._currentframe ]();
		}
		
	}
	
	function setTriggerEventByNum(nFrame:Number,fEvent:Function){
		_aEventList[nFrame ] = fEvent;

	}
	
	function clearTriggerEventByNum(nFrame:Number){
		_aEventList[nFrame ] = undefined;
	}
	
	function setTriggerEventByName(sFrame:String,fEvent:Function){
		//this is the crazyist way do to this but it's all I could think of
		var nFrameHolder = _currentframe
		this.gotoAndStop(sFrame);
		trace("Setting event on frame "+_currentframe);
		_aEventList[_currentframe ] = fEvent;
		if(_bPlaying) gotoAndPlay(nFrameHolder)
		else gotoAndStop(nFrameHolder);
	}
	
	function clearTriggerEventByName(sFrame:String){
		//this is the crazyist way do to this but it's all I could think of
		var nFrameHolder = _currentframe
		this.gotoAndStop(sFrame);
		_aEventList[_currentframe ] = undefined;
		if(_bPlaying) gotoAndPlay(nFrameHolder)
		else gotoAndStop(nFrameHolder);
	}


}